package org.mineground.player;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.mineground.Main;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @file PlayerLogManager.java (20.02.2012)
 * @author Daniel Koenen
 *
 */
public class PlayerLogManager {
    private static final Logger ExceptionLogger = LoggerFactory.getLogger(PlayerLogManager.class);
    
    public static final int ACTION_ID_BAN = 0;
    public static final int ACTION_ID_KICK = 1;
    public static final int ACTION_ID_UNBAN = 2;
    public static final int ACTION_ID_NOTE = 3;
    
    public static final String[] actionVerbs = {"ban", "kick", "unban", "note"}; 
    
    public static boolean addLogEntry(int actionId, String playerName, String adminName, String logReason) {
        Connection databaseConnection = Main.getInstance().getDatabaseHandler().getConnection();
        
        try {
            PreparedStatement preparedStatement = databaseConnection.prepareStatement("SELECT player_id FROM lvm_players WHERE login_name = ?");
            preparedStatement.setString(1, playerName);
            preparedStatement.execute();
            
            ResultSet queryResult = preparedStatement.getResultSet();
            
            if (!queryResult.next()) {
                return false;
            }
            
            int profileId = queryResult.getInt(1);
            preparedStatement = databaseConnection.prepareStatement("INSERT INTO lvm_player_log (player_id, action_id, entry_date, admin, reason) VALUES(?, ?, NOW(), ?, ?)");
            preparedStatement.setInt(1, profileId);
            preparedStatement.setInt(2, actionId);
            preparedStatement.setString(3, adminName);
            preparedStatement.setString(4, logReason);
            preparedStatement.execute();
            preparedStatement.close();
        }
        
        catch (Exception exception) {
            ExceptionLogger.error("Exception caught", exception);
        }
        
        return true;
    }
}
